<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
    <div>div</div>
    <p>p</p>
    <span>span</span>
    <i>i</i>
    <em>em</em>
    <div>div</div>
    <p>p</p>
    <span>span</span>
    <i>i</i>
    <em>em</em>
    <div>div</div>
    <p>p</p>
    <span>span</span>
    <i>i</i>
    <em>em</em>
    <div>div</div>
    <p>p</p>
    <span>span</span>
    <i>i</i>
    <em>em</em>
</body>
<script>
    // 封装是不是数组
    function isArr( array ){
        var length = array && array.length;
        return typeof length == 'number' && length >= 0;
    }
    // 封装each方法
    function each( array , callback ){
        var i,k;
        if ( isArr(array) ) {
            for ( i = 0 ; i < array.length ; i++ ) {
                if ( callback.call( array[i],i,array[i] ) === false ) break;
            }
        }else {
            for ( k in array ) {
                if ( callback.call( array[k] , k , array[k] ) ) break;
            }
        }
        return array;
    }
    // map方法
    function map( array,callback ) {
        var i,
                k,
                tmp,
                res = [];
        if ( isArray( array ) ) {
            for ( i = 0 ; i < array.length ; i++ ) {
                tmp = callback( array[i] , i );
                if ( tmp !== undefined ) {
                    res.push( tmp );
                }
            }
        }else {
            for ( k in array ) {
                tmp = callback( array[k] , k );
                if ( tmp !== undefined ) {
                    res.push( tmp );
                }
            }
        }
        return res;
    }
    // 获取元素
    function select(selector) {
        return document.querySelectorAll(selector);
    }
    /*************************************************************************/
    function Itcast(selector) {
        // 初始化 this 是伪数组
        var list = select(selector);
        [].push.apply( this,list );
        return this;

    }
    Itcast.prototype = {
        constructor: Itcast,
        each : function( callback ) {
            // 疆 this 作为数组，遍历数组，使用callback处理数组的每一个元素
            each( this,callback );
        },
        map: function(callback) {
            map( this,callback );
        }
    };
    // 使用
    new Itcast('div,span').each(function(){
        this.style.border = '1px solid red';
    });
</script>
</html>